package com.manor.paradise.dialog

import android.content.Context
import android.graphics.Color
import android.text.*
import android.text.method.PasswordTransformationMethod
import android.text.style.ForegroundColorSpan
import android.view.View
import com.bumptech.glide.Glide
import com.lxj.xpopup.impl.FullScreenPopupView
import com.manor.common.CommonAppConfig
import com.manor.common.http.JHttpCallback
import com.manor.common.utils.ClickUtil
import com.manor.common.utils.GsonUtil
import com.manor.common.utils.ToastUtil
import com.manor.paradise.R
import com.manor.paradise.bean.ParadiseInfoBean
import com.manor.paradise.bean.Record
import com.manor.paradise.http.ElvesHttpConst
import com.manor.paradise.http.ElvesHttpUtil
import kotlinx.android.synthetic.main.dialog_shopinfo_popup.view.*
import java.math.BigDecimal


/**
 * 购买交易
 * @property data Record
 * @constructor
 */
class ShopInfoDialog(context: Context, var data: Record) : FullScreenPopupView(context), View.OnClickListener {


    override fun getImplLayoutId(): Int {
        return R.layout.dialog_shopinfo_popup
    }

    override fun onCreate() {
        super.onCreate()
        im_dismiss.setOnClickListener(this)
        ed_mm.setInputType(InputType.TYPE_CLASS_NUMBER) //输入类型
        ed_mm.setFilters(arrayOf<InputFilter>(InputFilter.LengthFilter(6))) //最大输入长度
        ed_mm.setTransformationMethod(PasswordTransformationMethod.getInstance()) //设置为密码输入框
        ed_sdId.setText(data.sale.intValueExact().toString())
        if(data.sale.compareTo(BigDecimal("11"))== -1){

        }else{
            ed_purchase.setText(data.sale.intValueExact().toString())
            ed_purchase.setLongClickable(false);
            ed_purchase.setTextIsSelectable(false);
            ed_purchase.setEnabled(false)
            ed_purchase.setFocusable(false);
            tv_all.visibility = View.GONE
            if (!TextUtils.isEmpty(ed_purchase.text.toString())) {
                val number: BigDecimal = BigDecimal(ed_purchase.text.toString())
                val price: BigDecimal = BigDecimal(data.unit.toString())
                val Pay_price = number.multiply(price)
                tv_Pay_price.setText(Pay_price.toString())
            }
        }


        tv_yuehuan.setText(data.unit.toString())

        val ss = SpannableString("客官，我这里有上等的米粒置换  ，价格优惠手快有手慢无哦~")
        val foregroundColorSpan = ForegroundColorSpan(Color.parseColor("#0030FF"))
        ss.setSpan(foregroundColorSpan, 12, 14, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        tv_title.setText(ss)
        tv_all.setOnClickListener({
            ed_purchase.setText(data.sale.intValueExact().toString())
        })
        if(data.id.equals(CommonAppConfig.getInstance().getUid())){
            tv_title.setText("当前店铺为自己的店铺，无法购买")
            return
        }
        ed_purchase.addTextChangedListener(object : TextWatcher {
            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                if (!TextUtils.isEmpty(ed_purchase.text.toString())) {
                    val number: BigDecimal = BigDecimal(ed_purchase.text.toString())
                    val price: BigDecimal = BigDecimal(data.unit.toString())
                    val Pay_price = number.multiply(price)
                    tv_Pay_price.setText(Pay_price.toString())
                }else{
                    tv_Pay_price.setText("")
                }
            }

            override fun afterTextChanged(s: Editable?) {

            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

        })
        tv_confirm.setOnClickListener({
            if (!TextUtils.isEmpty(ed_purchase.text.toString())) {
                val number: BigDecimal = BigDecimal(ed_purchase.text.toString())
                val available: BigDecimal = BigDecimal(data.sale.toString())
                val sdCouponsNum: BigDecimal = BigDecimal(tv_Pay_price.text.toString())

                if (number.compareTo(available) == 1) {
                    ToastUtil.show("购买数量不能大于店铺置换数量")
                } else {
                    if (TextUtils.isEmpty(ed_mm.text.toString()) || ed_mm.text.toString().length != 6) {
                        ToastUtil.show("请输入交易密码")
                        return@setOnClickListener
                    }
                    val multiple = if (data.shopType == 1) 1 else 10
                    if (Integer.parseInt(ed_purchase.text.toString()) % multiple !== 0) {

                        ToastUtil.show("数量需要是10的倍数")
                    return@setOnClickListener
                    }
                    tv_confirm.setClickable(false)
                    ElvesHttpUtil.getBuyingOre(number, ed_mm.text.toString(), sdCouponsNum, BigDecimal(data.id), object : JHttpCallback() {
                        override fun onSuccess(code: Int, msg: String, info: String) {
                            tv_confirm.setClickable(true)
                            if (code != 200) {
                                ToastUtil.show(msg)
                                return
                            }
                            dismiss()

                        }

                        override fun onError() {
                            super.onError()
                            tv_confirm.setClickable(true)
                        }
                    })
                }

            }else{
                if(TextUtils.isEmpty(ed_purchase.text.toString())){
                    ToastUtil.show("请输入数量")
                }
            }
        })

        ElvesHttpUtil.getParadiseInfo( object : JHttpCallback() {
            override fun onSuccess(code: Int, msg: String, info: String) {
                if (code != 200) {
                    ToastUtil.show(msg)
                    return
                }
                var paradiseInfoBean: ParadiseInfoBean = GsonUtil.gsonToBean(info, ParadiseInfoBean::class.java)
                tv_ks.setText("我有淘米劵："+ paradiseInfoBean.sdCoupons.toString())
            }

            override fun onError() {
                super.onError()
            }
        })
    }


    override fun onClick(v: View?) {
        if (ClickUtil.canClick(500))
            when (v?.id) {
                R.id.im_dismiss -> {
                    dismiss()
                }
            }
    }
    override fun onDestroy() {
        Glide.get(context).clearMemory()

        ElvesHttpUtil.cancel(ElvesHttpConst.POST_OPEN_SHOP_T_SELL)
        ElvesHttpUtil.cancel(ElvesHttpConst.POST_OPEN_SHOP_BY_UID)
        super.onDestroy()
    }
}